define(['moment'], function (moment) {
    return ['$scope', '$filter', '$stateParams', 'config', 'MMWService', '$calculater',
    function ($scope, $filter, $stateParams, config, $MMWService, $calculater) {
        $scope.init = function(){
            $scope.wo_no = '';
        };

        $scope.back = function(){
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);
        };

        $scope.barcodeScan = function(event){
            if(event.keyCode == 13){
                $scope.search();
            }
        };

        $scope.search = function(){
            if($scope.wo_no ==''){
                $scope.$root.showAlert($filter('translate')('B0105.msg.wo_no_empty'));
                return;
            }

            $MMWService.sendToServer({
                uri: 'ServicesSTD.Module_WIP.plot_release_info_get',
                content : {
                    wo_no : $scope.wo_no //工單號碼
                },
                success : function(data){
                    $scope.wo_qty = data.wo_qty;    //工單數量
                    $scope.unrelease_qty = data.unrelease_qty;    //工單未下線數量
                    $scope.customer_no = data.customer_no;    //客戶編號
                    $scope.plan_sdate = data.plan_sdate;    //預計開始時間
                    $scope.plan_edate = data.plan_edate;    //預計結束時間
                    $scope.plot_info_detail = data.plot_info_detail;    //生產批清單
                    $scope.CheckUNDispatch = data.CheckUNDispatch;
                },
                error: function(data){
                    $scope.$root.showAlert(data.Message);
                    $scope.wo_qty = '';
                    $scope.unrelease_qty = '';
                    $scope.customer_no = '';
                    $scope.plan_sdate = '';
                    $scope.plan_edate = '';
                    $scope.plot_info_detail = [];
                    $scope.$root.focusById('wo_no');
                }
            });
        };

        $scope.load_wo_noList = function(){
            if($scope.wo_no.length < 5){
                $scope.$root.showAlert($filter('translate')('B0101.errorMsg.wo_no_length'));
                return;
            }
            $MMWService.showQueryListPNIG({
                page_mode: 1,
                pageRecord:50,
                notQuery: ($scope.wo_no == '') ? true : false,
                condition: [
                    {name:$filter('translate')('common.queryList.wo_c'), column_name:'MONo', query_mode:'2', value: $scope.wo_no}
                ],
                header: [
                    {label:$filter('translate')('common.queryList.wo_c'), code:'page_no'}
                ],
                setCondition:function(conditions){
                    conditions.push({column_name:'MOSTATE', query_mode:'0', value:3, merge_condition_model:'AND' });
                    return conditions;
                },
                selectCode: $scope.wo_no,
                confirm : function(selectItem){
                    $scope.wo_no = selectItem.page_no;
                    $scope.search();
                }
            });
        };

        $scope.confirm = function(){
            var msg = validate();
            if(msg !=''){
                $scope.$root.showAlert(msg);
                return;
            }

            var release_detail = [];
            $scope.plot_info_detail.forEach(function(item){
                if(item.modi_release_qty && item.modi_release_qty >= 0){
                    release_detail.push({
                        plot_no: item.plot_no,
						status:item.status,
                        release_qty: item.modi_release_qty || item.release_qty,
						ori_release_qty:item.release_qty
                    });
                }
            });

            // 20200526 modify by WeiTing for M#72378 - 增加檢核，是否生產批皆已下線或是尚未調整修改數量。
            if(release_detail && release_detail.length > 0)
            {
                 // 20221219 modify by xingl for M#0114994: [sMES] 生產批下線數量調整後，派工表數量異常 [英發]
                 if($scope.CheckUNDispatch == "1"){
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_WIP.plot_release_process',
                        content : {
                            wo_no : $scope.wo_no, //工單號碼
                            release_detail : release_detail //下線清單
                        },
                        success : function(data){
                            $scope.$root.showToast($filter('translate')('B0105.msg.success'));
                            $scope.search();
                        }
                    });
                }
                else{
                    // 生產批下線數量調整後，派工計畫將清除需重新派工！
                    $scope.$root.showConfirm($filter('translate')('B0105.msg.column01'), function(dialog){
                        $MMWService.sendToServer({
                            uri: 'ServicesSTD.Module_WIP.plot_release_process',
                            content : {
                                wo_no : $scope.wo_no, //工單號碼
                                release_detail : release_detail //下線清單
                            },
                            success : function(data){
                                dialog.hide();
                                $scope.$root.showToast($filter('translate')('B0105.msg.success'));
                                $scope.search();
                            }
                        });
                    });
                }
                
            } 
            else
            {
                // 生產批皆已下線或尚未調整數量！
                $scope.$root.showAlert($filter('translate')('B0105.msg.offline_or_not_adjust'));
            };

        };

        function validate(){
            var msg = '',
                ori_release_qty = 0, //修改前的下線數量加總
                new_release_qty = 0; //修改後的下線數量加總
            if((+$scope.unrelease_qty)>=0){
                $scope.plot_info_detail.forEach(function(item){
                    ori_release_qty = $calculater.decimalAdd(ori_release_qty, item.release_qty);
                    new_release_qty = $calculater.decimalAdd(new_release_qty, (item.modi_release_qty || item.release_qty)); //先取下線數量(修)，沒有則取下線數量作加總
                });

                if((+new_release_qty) > (+$scope.wo_qty)){
                    msg = $filter('translate')('B0105.msg.wo_qty_less_than_release_qty', {release_qty: new_release_qty, wo_qty: $scope.wo_qty});
                }else if($calculater.decimalSub(new_release_qty, ori_release_qty) > (+$scope.unrelease_qty)){
                    msg = $filter('translate')('B0105.msg.unrelease_qty_less_than_release_qty', {release_qty: new_release_qty, unrelease_qty: $scope.unrelease_qty});
                }
            } else {
                msg = $filter('translate')('B0105.msg.unrelease_qty_error', {un_release_qty: $scope.unrelease_qty});
            }
            return msg;
        }
    }
];});
